package com.fairytask.engine.components.text
{
	import com.fairytask.engine.components.BaseComponent;
	import com.fairytask.engine.define.ColorDefine;
	import com.fairytask.engine.define.Filter;
	import com.fairytask.engine.utils.draw.DrawAPI;
	import com.fairytask.resource.StaticIni;
	
	import flash.filters.GlowFilter;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	/**
	 * 用于显示简单文本，文本不能产生交互，只能进行值的设置 
	 * @author Hanjy
	 * 
	 */	
	public class Label extends BaseComponent
	{
		private var _htmlStr	:String	=	"";
		private var _color		:String		=	"FFCB4A";
		private var _labelFilter	:GlowFilter;
		private var _tf	:TextFormat	=	null;
		private var _hasBorder	:Boolean = false;
		
		public function Label(txtColor:String="FFCB4A",size:int=12)
		{
			_labelFilter	=	Filter.COMMON_LABEL_FILTER;
			
			_tf				=	new TextFormat();
			_tf.font		=	"微软雅黑";
			
			if(size!=12)
			{
				_tf.color		=	txtColor;
//				_tf.size		=	18;
				_tf.size		=	size;
			}
			if(StaticIni.getIniVar("textColor",txtColor)!=null)
			{
				_color	=	StaticIni.getIniVar("textColor",txtColor);
			}
			else
			{
				_color	=	txtColor;
			}
		}
		
		public function get color():String
		{
			return _color;
		}

		public function set color(value:String):void
		{
			_color	= value;
			//不可用的时候重绘不要加边，太深
			if(_color==ColorDefine.DISABLE)
			{
				drawView(false);
			}
			else
			{
				drawView(_hasBorder);
			}
		}

		public function set htmlStr(val:String):void
		{
			if(val == "")
			{
				val	=	" ";
			}
			_htmlStr	=	val;
			_hasBorder = false;
			drawView(_hasBorder);
		}
		
		public function get htmlStr():String
		{
			return _htmlStr;
		}
		
		public function set htmlStrByBorder(val:String):void
		{
			if(val == "")
			{
				val	=	" ";
			}
			_htmlStr	=	val;
			_hasBorder = true;
			drawView(_hasBorder);
		}
		
		/**
		 * 画出文字，并加到舞台上 
		 * @param hasFilter
		 * 
		 */
		private function drawView(hasFilter:Boolean=false):void
		{
			var	str:String	= StaticIni.getSwfLangStrVar("textcolor",[_color,_htmlStr]);
			clearView();
			var filterArr	:Array	=	[];
			if(hasFilter&&_labelFilter)
			{
				filterArr	=	[_labelFilter];
			}
			
			addChild(DrawAPI.getStringBitmap(str,{
				autoSize:TextFieldAutoSize.LEFT,
				multiline:true,
				wordWrap:false,
				filters:filterArr
			},_tf));
		}
		
		public function get htmlStrByBorder():String
		{
			return _htmlStr;
		}
		
		public function set htmlStrByInt(val:*):void
		{
			var	str:String;
			
			if(val is int)
			{
				str = 	String(val);
			}
			else if (val is Number)
			{
				str =	String(Math.ceil(val));
			}
			_htmlStr		=	str;
			
			var	showStr:String	=	StaticIni.getSwfLangStrVar("textcolor",["#"+_color,str]);
			
			clearView();
			
			addChild(DrawAPI.getStringBitmap(showStr));
		}
		
		private function clearView():void
		{
			while(numChildren>0)
			{
				removeChildAt(0);
			}
		}

		public function get labelFilter():GlowFilter
		{
			return _labelFilter;
		}

		public function set labelFilter(value:GlowFilter):void
		{
			_labelFilter = value;
		}
		public function set tf(val:TextFormat):void
		{
			_tf = val;
		}

	}
}